package com.lzwork.gpt.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.lzwork.gpt.db.entity.ApiEntity;
import com.lzwork.gpt.service.ApiDataService;
import com.lzwork.utility.logger.LOGGER;
import com.lzwork.utility.pair.StringPair;
import com.lzwork.utility.str.StrUtility;
import com.lzwork.web.BaseController;

/**
 * @author LiZheng
 * @date 2023年12月26日 14:05:45
 */

@Controller
@RequestMapping(value = "/job/*")
public class ApiJobController extends BaseController
{
	@Autowired
	ApiDataService service;

	public static String apiKeyToUrl(String server, String apikey)
	{
		String fullUrl = server;
		if (!fullUrl.endsWith("/"))
			fullUrl += "/";
		fullUrl += "job/callapi?apikey=" + apikey;
		return fullUrl;
	}

	@GetMapping(value = "/")
	@ResponseBody
	public JSONObject info(HttpServletResponse response, @RequestParam(value = "key", defaultValue = "") String key)
	{
		JSONObject result = new JSONObject();

		if (StrUtility.stringValid(key))
		{
			ApiEntity api = service.getApi(key);
			if (api == null)
				throwCode500(response, "无效的API KEY: [" + key + "]");
		} else
			key = "NULL";

		result.put("key", key);

		return result;
	}

	@PostMapping(value = "/callapi", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject callapi(@RequestBody Map<?, ?> map, @RequestParam String apikey)
	{
		if (apikey.endsWith("/"))
			apikey = apikey.substring(0, apikey.length() - 1);

		ApiEntity api = service.getApi(apikey);
		if (api == null)
			return errReturn("无效的API KEY: [" + apikey + "]");

		boolean dolog = false;
		Boolean doLogData = getMapBoolean(map, "dolog", null, false);
		if (doLogData != null)
		{
			dolog = doLogData;
			LOGGER.log(LOGGER.NET, "Recieving setting [dolog=" + doLogData + "] from caller");
		}

		List<StringPair> paramData = ApiEntity.apiParamToCallParam(map);

		return service.apiCall(api, null, paramData, false, dolog);
	}

	@PostMapping(value = "/makeprompt", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject makeprompt(@RequestBody Map<?, ?> map, @RequestParam String apikey)
	{
		if (apikey.endsWith("/"))
			apikey = apikey.substring(0, apikey.length() - 1);

		ApiEntity api = service.getApi(apikey);
		if (api == null)
			return errReturn("无效的API KEY: [" + apikey + "]");

		List<StringPair> paramData = ApiEntity.apiParamToCallParam(map);
		String prompt = api.makePrompt(paramData, true);

		JSONObject result = new JSONObject();
		result.put("prompt", prompt);

		return result;
	}
}
